 package com.yixun.qfbao.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.yixun.common.controller.BaseController;
import com.yixun.common.exception.YIXUNExceptionCode;
import com.yixun.common.exception.YIXUNUNCheckedException;
import com.yixun.qfbao.util.FileUploadPorxyUtil;

@Controller
@RequestMapping("/qfbao/common")
public class CommonController extends BaseController{
	private static Logger log = Logger.getLogger(CommonController.class);
	@Value("#{configProperties['file.serviceUrl']}")
	public String  serviceUrl;
	/**
	 * 上传文件(apk或者UI)
	 * @param picture
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/uploadFile")
	public String uploadFile(Model model,MultipartFile file,String type,HttpServletResponse response) throws IOException{
		response.setContentType("text/html; charset=utf-8");
		JSONObject res = new JSONObject();
		try {
			if(file.getSize()==0){
				JSONObject json = new JSONObject();
				json.put("result", "0");
				json.put("errorMsg", "未选中任何文件");
				response.getWriter().append(json.toJSONString());
				return null;
			}
			JSONObject params = new JSONObject();
			params.put(type, type);
			String result = FileUploadPorxyUtil.submitPost(serviceUrl,params, file);
			response.getWriter().append(result);
			return null;
		}catch (YIXUNUNCheckedException e) {
			log.error("上传失败",e);
			res.put("result","0");
			res.put("errorCode",e.getCode());
			res.put("errorMsg",e.getOutMsg());
		}catch (Exception e) {
			log.error("上传失败",e);
			res.put("result","0");
			res.put("errorCode",YIXUNExceptionCode.UNKNOW_EXCEPTON.getCode());
			res.put("errorMsg",YIXUNExceptionCode.UNKNOW_EXCEPTON.getDesout());
		}
		model.addAttribute("apires",res);
		return "apires";
	}
}
